<!--
 * @Version: 1.0
 * @Autor:
 * @Date: 2021-03-10 16:57:07
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-09-13 14:22:39
 * @Description:
-->
<template>
  <el-card class="app-container content_box" shadow="always">
    <div>
      <!-- title -->
      <!-- <div class="header_title_box">
        <h2>币别管理</h2>
      </div> -->
      <!-- <div class="select_form_box">
        <div class="filter-container">
          <el-input v-model="queryParam.name" size="small" placeholder="币别名称" class="select250 mr16" @keyup.enter.native="handleSearch" />
        </div>
      </div> -->
      <form-selections :is-active="isActive" :items="formItems" :query-param.sync="queryParam" display="block" @handleSearch="handleSearch" />
      <div class="select_flex">
        <div class="operation_button_box">
          <el-button v-action="'currency@save'" icon="el-icon-circle-plus" size="small" type="success" @click="handleCreate()">新增币别</el-button>
          <el-button v-action="'currency@disable'" :disabled="selectedIds.length?false:true" size="small" type="danger" @click="handleDisable()">禁用</el-button>
          <el-button v-action="'currency@enable'" :disabled="selectedIds.length?false:true" size="small" type="primary" @click="handleEnable()">启用</el-button>
        </div>
        <div>
          <el-button size="small" type="primary" class="filter-item search" icon="el-icon-search" @click="handleSearch">
            搜索
          </el-button>
          <el-button size="small" icon="el-icon-refresh" @click="handleRefresh">重置</el-button>
        </div>
      </div>
      <div v-loading="loading">
        <el-table ref="multipleTable" :data="data" tooltip-effect="dark" style="width: 100%" :max-height="screenHeigh" border fit @selection-change="handleSelectMulti">
          <el-table-column align="center" type="selection" width="55" />
          <el-table-column label="操作" align="center" fixed width="200px">
            <template slot-scope="scope">
              <span v-if="scope.row.is_status===2" v-action="'currency@update'" class="edit_style" @click="handleUpdateNew(scope.row)">编辑</span>
              <span v-if="scope.row.is_status===2" v-action="'currency@delete'" class="delete_style" @click="handleDelete(scope.row.id)">删除</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="币别代码" prop="source_code" />
          <el-table-column align="center" label="币别" prop="source_name" />
          <el-table-column align="center" label="汇率" prop="rate" />
          <el-table-column align="center" prop="is_status" label="状态">
            <template slot-scope="scope">
              {{ scope.row.is_status===1?'启用':'禁用' }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="creator_name" label="创建人" />
          <el-table-column align="center" prop="created_at" label="创建时间" width="165" />
          <el-table-column align="center" prop="update_name" label="修改人">
            <template slot-scope="scope">
              {{ scope.row.update_name?scope.row.update_name:'-' }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="updated_at" label="更新时间" width="165" />
        </el-table>
        <el-pagination v-if="paginate.total > 0" class="pagination_box" background :current-page="paginate.current" :page-sizes="[10, 20, 30, 40]" layout="total, sizes, prev, pager, next, jumper" :total="paginate.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
      <!-------- 回到顶部 ------------>
      <el-backtop :visibility-height="60">
        <i class="el-icon-caret-top" />
      </el-backtop>
      <el-dialog :center="true" :title="title" :visible.sync="formVisible" width="45%" @close="handleCancel">
        <el-form :ref="formName" :model="formFieldsData" :rules="rules">
          <div class="header_title_box">
            <h2>币别信息</h2>
          </div>
          <el-form-item label="币别代码" :label-width="formLabelWidth" prop="source_code">
            <el-input v-model.trim="formFieldsData.source_code" maxlength="3" oninput="value=value.replace(/[\u4E00-\u9FA5]/g,'')" placeholder="请输入币别代码" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="币别名称" :label-width="formLabelWidth" prop="source_name">
            <el-input v-model.trim="formFieldsData.source_name" placeholder="请输入币别名称" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="汇率" :label-width="formLabelWidth" prop="rate">
            <el-input v-model.trim="formFieldsData.rate" placeholder="请输入汇率" maxlength="9" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="备注说明" :label-width="formLabelWidth" prop="remarks">
            <el-input v-model="formFieldsData.remarks" type="textarea" autosize show-word-limit autocomplete="off" placeholder="请输入备注说明" clearable :autosize="{ minRows: 5, maxRows: 10}" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer-center">
          <el-button @click="handleCancel">取 消</el-button>
          <el-button type="primary" @click="handleSubmitNew">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </el-card>

</template>

<script>
import formOperate from '@/layout/mixin/formOperate'
import {
  action
} from '@/directive/permission/index.js' // 权限判断指令
import cacheList from '@/layout/mixin/cacheList'
export default {
  name: 'BasicsCurrencys',
  directives: {
    action
  },
  mixins: [formOperate, cacheList],
  data() {
    return {
      otherHeight: 365,
      componentName: 'BasicsCurrencys',
      url: '/currency',
      queryParam: {
        status: '',
        name: ''
      },
      formItems: [
        {
          label: '币别名称',
          key: 'name',
          type: 'input',
          placeholder: '请输入币别名称'
        }
      ],
      option: [{
        value: 2,
        label: '禁用'
      },
      {
        value: 1,
        label: '启用'
      }
      ],
      loading: false,
      formVisible: false,
      formFieldsData: {
        source_code: '', // 店铺编号
        source_name: '',
        rate: '',
        remarks: ''
      },
      formName: 'currencys',
      formLabelWidth: '150px',
      rules: {
        source_code: [
          { required: true, message: '请输入币别代码', trigger: 'blur' },
          { pattern: /^[A-Za-z]{3}$/, message: '请输入正确的币别代码', trigger: 'change' }
        ],
        source_name: [{ required: true, message: '请输入币别名称', trigger: 'blur' }],
        rate: [
          { required: true, message: '请输入币别汇率', trigger: 'blur' },
          { pattern: /\d+(\.[0-9]{1,4}){0,1}$/, message: '只能输入数字,最多四位小数', trigger: 'change' }]
      }
    }
  },
  created() {
    // const vm = this
  },
  methods: {
    beforeCreate() {
      this.formFieldsData.id = null
    },
    handleUpdateNew(data) {
      this.$http.get(`currency/${data.id}`).then(res => {
        const dataObj = res.data
        this.formFieldsData = dataObj
        this.formVisible = true
      })
    },
    handleSubmitNew() {
      this.$refs['currencys'].validate((valid) => {
        if (valid) {
          // 编辑
          if (this.formFieldsData.id) {
            this.$http.put(`currency/${this.formFieldsData.id}`, this.getData()).then(response => {
              if (response.code === 10000) {
                this.$message.success('编辑成功')
                this.formVisible = false
                this.handleRefresh()
              } else {
                this.$message.error(response.message)
              }
            })
          } else {
            // 判断个人信息
            this.$http.post('currency', this.getData()).then(response => {
              if (response.code === 10000) {
                this.$message.success('添加成功')
                this.formVisible = false
                this.handleRefresh()
              } else {
                this.$message.error(response.message)
              }
            })
          }
        }
      })
    },
    // 获取添加参数组合
    getData() {
      const data = {
        source_code: this.formFieldsData.source_code,
        source_name: this.formFieldsData.source_name,
        rate: this.formFieldsData.rate,
        remarks: this.formFieldsData.remarks
      }
      return data
    }
  }
}

</script>

<style lang="scss" scoped>
</style>
